from peewee import *

# db = SqliteDatabase('people.db')

# 使用mysql 连接
db = MySQLDatabase('people', user='root', password='huyu1680456489', host='localhost', port=3306)


class Person(Model):
    name = CharField()
    birthday = DateField()

    class Meta:
        database = db


class Pet(Model):
    owner = ForeignKeyField(Person, backref='pets')
    name = CharField()
    animal_type = CharField()

    class Meta:
        database = db


if __name__ == '__main__':
    # db.connect()
    # db.create_tables([Person, Pet])

    # from datetime import date
    #
    # uncle_bob = Person(name='salary', birthday=date(1971, 1, 15))
    # uncle_bob.save()

    uncle_bob = Person.get(name='Bob')
    # uncle_bob.delete_instance()  # 删除操作

    # for i in Person.select():  # 获取所有的数据
    #     print(i.name)

    # 获取所有的数据
    # pet = Pet(owner=uncle_bob, name='大黄', animal_type='狗')
    # pet.save()
    # query = Pet.select().where(Pet.animal_type == '猫')
    # for i in query:
    #     print(i.name)
    #
    # # 获取Bob的所有宠物
    # print(Pet.select().join(Person).where(Person.name == 'Bob'))
    # for pet in Pet.select().join(Person).where(Person.name == 'Bob'):
    #     print(pet.name)

    # subquery = Pet.select(fn.COUNT(Pet.id)).where(Pet.owner == Person.id)

    # for person in query.aggregate_rows():
    #     print()


